/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package management;

import java.io.*;

/**
 *
 * @author Cucnth
 */
public class ManipulationFile
{
    private static String changePath(String pathStart)
    {
        return pathStart.substring(0, pathStart.lastIndexOf("\\"));
    }

    private static String changePath(String path, String pathStart, String pathEnd)
    {
        return path.replace(pathStart, pathEnd);
    }

    private static String changePath(String path, String fileName)
    {
        int start = path.lastIndexOf("\\");
        int end = path.lastIndexOf(".");
                
        return path.replace(path.substring(start, end), fileName);
    }

    public static boolean createNewFolder(String path, boolean isFile) throws Exception
    {
        try
        {
            if(isFile)
            {
                if(path.lastIndexOf("\\") != -1)
                {
                    path = path.substring(0, path.lastIndexOf("\\"));
                }
                else if(path.lastIndexOf("/") != -1)
                {
                    path = path.substring(0, path.lastIndexOf("/"));
                }
            }
            
            File file = new File(path);

            if(!file.exists())
            {
                if(file.mkdirs())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static boolean delete(String path) throws Exception
    {
        try
        {
            File file = new File(path);

            if(file.exists())
            {
                if(file.delete())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static void copy(String pathStart, String pathEnd, String fileName) throws Exception
    {
        try
        {
            IsFile list = new IsFile();
            ReadFile readFile = new ReadFile(list, pathStart);
            String path = changePath(pathStart, changePath(pathStart), pathEnd);

            if(fileName != null)
            {
                path = changePath(path, fileName);
            }

            if(createNewFolder(path, true))
            {
                WriteFile writeFile = new WriteFile(list, path);

                readFile.start();
                writeFile.start();
            }
            else
            {
                throw new Exception("Canot create a new folder!");
            }
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static void copy(String pathStart, String pathEnd) throws Exception
    {
        try
        {
            copy(pathStart, pathEnd, null);
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static boolean checkFileExist(String path, boolean createNewFile) throws Exception
    {
        try
        {
            if(createNewFolder(path, true))
            {
                File file = new File(path);

                if(createNewFile)
                {
                    file.createNewFile();
                }

                return file.exists();
            }
            else
            {
                return false;
            }            
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static String readFromFile(String path) throws Exception
    {
        try
        {
            String s = "";

            if(checkFileExist(path, false))
            {
                BufferedReader br = new BufferedReader(new FileReader(path));
                String line = "";

                while((line = br.readLine()) != null)
                {
                    s += line + "\n";
                }

                br.close();
            }
            else
            {
                throw new Exception("File not found!");
            }

            return s;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public static void saveToFile(String path, String content, boolean append) throws Exception
    {
        try
        {
            FileWriter fw = new FileWriter(path, append);

            checkFileExist(path, true);
            
            fw.write(content);
            fw.close();
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
}
